package cn.tedu.api;

//测试 Object工具类
//Object的toString()
//Object的equals()
import java.util.Objects;

public class Test1_Object {
    public static void main(String[] args) {
        Student s1 = new Student("张三君",20);
        System.out.println(s1.name);
        System.out.println(s1.age);
        System.out.println(s1);//如果不重写toString()这里打印的就是地址值。
        Student s2 = new Student("法外狂徒",25);
        System.out.println(s2.name);
        System.out.println(s2.age);
        System.out.println(s2);
        System.out.println(s1.equals(s2));


    }
}
class Student{
    String name;
    int age;
    public Student(){}
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }
//重写toString()
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    //重写equals
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
